﻿<?php
session_start();

require_once("conf/configuration.inc");
$section = isset($_GET["section"]) ? $_GET["section"] : DEFAULT_SECTION;

require_once(LIBRARIES_DIR."Utils.php");
require_once(LIBRARIES_DIR."ContentManager.php");
require_once(LIBRARIES_DIR."DBManager.php");

// Establecemos el idioma
$lang = Utils::setLanguage();

// Conectamos con la bd
$db = new DBManager();
$db->connect();

// Comprobamos si la página tiene que ir con ssl o no
if (CHECK_PROTOCOL == 1) {
	Utils::checkProtocol($section);
}

// Pintamos el contenido
$content = new ContentManager($lang);

// ZONA DE ADMINISTRACIÓN
if ($section == "admin")
{
	$content->adminInitiate();
	include(INCLUDES_DIR."admin-".$_GET["subsection"].".php");
	$content->display("admin-".$_GET["subsection"].".tpl");
}
else
{
	$secureSections = explode(",", SECURE_SECTIONS);
	$ssl = in_array($section, $secureSections) ? true : false;
	$content->initiate($ssl);

	define("PAGE_URL", SERVER_URL.$_SERVER["REQUEST_URI"]);
	$content->assign("pageurl", PAGE_URL);
	$content->assign("sendtofriendaction", BASE_URL.$lang."/process-sendtofriend-form/");

	// Marca de campo obligatorio
	$code = "<b>*</b>";
	$content->assign("mandatoryfieldmarker", $code);
	$content->assign("mandatoryfieldsnote", $content->langmsg["mandatoryfieldsnote"]);

	if (!isset($_GET["subsection"]))
	{
		include(INCLUDES_DIR.$section.".php");

		if (strpos($section, "process-") === false)
		{
			$content->assign("pagealias", $section);
			$content->assign("pagetitle", $content->langmsg[$section]);
			$content->assign("section", $section);
			$content->assign("subsection", "");

			// Migas de pan
			if ($section == "home") {
				$content->assign("breadcrumbs", "Inicio");
			} else {
				$code1 = "<a href=\"".BASE_URL.$lang."\">Inicio</a>";
				$code2 = " &raquo; ".$content->langmsg[$section];
				$content->assign("breadcrumbs", $code1.$code2);
			}

			// Metas
			$query = "SELECT description, keywords
					  FROM sections
					  WHERE alias = '$section'";
			$resMetas = $db->runQuery($query);
			if ($db->getRowCount($resMetas)) {
				$rowMetas = $db->getRow($resMetas);
				$content->assign("pagedescription", utf8_encode($rowMetas["description"]));
				$content->assign("keywords", utf8_encode($rowMetas["keywords"]));
			} else {
				$content->assign("pagedescription", $content->langmsg["defaultpagedescription"]);
				$content->assign("keywords", $content->langmsg["defaultkeywords"]);
			}

			$content->display($section.".tpl");
		}
	} 
	else
	{
		$subsection = $_GET["subsection"];
		switch ($section) {
			case "esencias":
				include(INCLUDES_DIR.$subsection.".php");
				break;
			case "blog":
				include(INCLUDES_DIR."blogentry.php");
				break;
			case "tours":
				include(INCLUDES_DIR."tour.php");
				break;
			case "info":
				include(INCLUDES_DIR."info-section.php");
				break;
		}
		$content->assign("pagealias", $section);
		$content->assign("pagetitle", $pagetitle);
		$content->assign("section", $section);
		$content->assign("subsection", $subsection);

		// Migas de pan
		$code1 = "<a href=\"".BASE_URL.$lang."\">Inicio</a>";
		$code2 = " &raquo; <a href=\"".BASE_URL.$lang."/".$section."\">".$content->langmsg[$section]."</a>";
		$code3 = " &raquo; ".($section == "blog" ? $pagetitle : $content->langmsg[$subsection]);
		$content->assign("breadcrumbs", $code1.$code2.$code3);

		// Metas
		//$query = "SELECT description, keywords FROM metas WHERE section = '$section' AND subsection = '$subsection' AND language = '$lang'";
		$query = "SELECT description, keywords
				  FROM sections
				  WHERE alias = '$section'";
		$resMetas = $db->runQuery($query);
		if ($db->getRowCount($resMetas)) {
			$rowMetas = $db->getRow($resMetas);
			$content->assign("pagedescription", utf8_encode($rowMetas["description"]));
			$content->assign("keywords", utf8_encode($rowMetas["keywords"]));
		} else {
			$content->assign("pagedescription", $content->langmsg["defaultpagedescription"]);
			$content->assign("keywords", $content->langmsg["defaultkeywords"]);
		}

		$content->display($tplname);
	}
}
?>
